package edu.sjb.map;

import java.awt.Graphics;
import java.awt.Rectangle;

import edu.sjb.Drawable;
import edu.sjb.characters.GameCharacter;
import edu.sjb.characters.Mario;
import edu.sjb.gui.GameScreen;

public abstract class GameEntity implements Runnable, Cloneable {
	protected int sizeX;
	protected int sizeY;
	protected int posX;
	protected int posY;
	protected double speedX;
	protected double speedY;
	protected Rectangle collisionArea;
	protected Drawable image;
	protected GameScreen gameContext;

	protected GameEntity() {
		sizeX = 0;
		sizeY = 0;
		posX = 0;
		posY = 0;
		image = null;
		gameContext = null;
		speedX = 20;
	}

	public void shiftPosX(double delta) {
		posX += (delta * speedX) / 100;
	}

	public void setPosX(int px) {
		posX = px;
	}

	public void setPosY(int py) {
		posY = py;
	}

	public void setSizeX(int sx) {
		sizeX = sx;
	}

	public void setSizeY(int sy) {
		sizeY = sy;
	}

	public void setImage(Drawable img) {
		image = img;
	}

	public void setGameContext(GameScreen gs) {
		gameContext = gs;
	}

	public void setSpeedX(double sx) {
		speedX = sx;
	}

	public void setSpeedY(double sy) {
		speedY = sy;
	}

	public void setCollisionArea(Rectangle ca) {
		collisionArea = ca;
	}

	public void dibujar(Graphics g) {
		image.draw(g, posX, posY);
	}

	public abstract void hitAgainstMario(Mario m);

	public abstract void characterCollide(GameCharacter gc);

	public Object clonate() {
		try {
			return clone();
		}
		catch (CloneNotSupportedException e) {
		}
		return null;
	}

	public int posX() {
		return posX;
	}

	public int posY() {
		return posY;
	}

	public int sizeX() {
		return sizeX;
	}

	public int sizeY() {
		return sizeY;
	}

	public double speedX() {
		return speedX;
	}

	public double speedY() {
		return speedY;
	}

	public Rectangle collisionRectangle() {
		return collisionArea;
	}

	public boolean collides(GameEntity ge) {
		Rectangle thisArea = new Rectangle();
		Rectangle otherArea = new Rectangle();
		thisArea.setBounds(posX(), posY(), sizeX, sizeY);
		otherArea.setBounds(ge.posX(), ge.posY(), ge.sizeX(), ge.sizeY());
		setCollisionArea(thisArea);
		ge.setCollisionArea(otherArea);
		return thisArea.intersects(otherArea);
	}

	public Drawable image() {
		return image;
	}

	public GameScreen game() {
		return gameContext;
	}
}
